//
//  ShouCangViewController.m
//  SnailPost
//
//  Created by Liuiu on 16/3/15.
//  Copyright © 2016年 抠泥ser. All rights reserved.
//

#import "ShouCangViewController.h"
#import "ShouCangCell.h"
#import "LiuiuCoreData.h"
#import "Entity+CoreDataProperties.h"
#import "PrefixHeader.pch"
#import "ShouCangMoreVC.h"

@interface ShouCangViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UISegmentedControl *seg; //分段
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation ShouCangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    [self.view NightWithType:UIViewColorTypeNormal];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    self.navigationController.navigationBar.translucent = NO;
    

    
//******设置tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //注册重用池
    [_tableView registerClass:[ShouCangCell class] forCellReuseIdentifier:@"cell"];
    [self data];
    
}
- (void)data
{
    self.dataArr = [NSMutableArray arrayWithArray: [[LiuiuCoreData defaultsCoreDataManager]selectAllModel]];
    
    [_tableView reloadData];

}



// 编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
}

//向左滑动删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    Entity *vi = _dataArr [indexPath.row];
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_dataArr removeObjectAtIndex:indexPath.row];
        //向左滑动删除
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        //把数据库里的数据也删除了
        [[LiuiuCoreData defaultsCoreDataManager]deleteModelWithKey:@"title" value:vi.title];
        [[LiuiuCoreData defaultsCoreDataManager]saveContext];
}

}

// 删除确认 信息设置
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


//设置tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    ShouCangCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     Entity *video = _dataArr[indexPath.row];
    cell.title.text = video.title;
    [cell.image sd_setImageWithURL:[NSURL URLWithString:video.cover]];
    cell.times.text = video.length.stringValue;
    cell.counts.text = video.playCount.stringValue;
    
    return cell;
}

//点击实现方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShouCangMoreVC *sec =[[ShouCangMoreVC alloc]init];
    Entity *m = _dataArr[indexPath.row];
    sec.hm = m;
    
    
    [self presentViewController:sec animated:YES completion:^{
        
    }];
}


//设置cell宽度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
